/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package action.activity;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import po.Activity;
import po.User;
import java.util.List;
import java.util.Set;
import myUtils.Constants;
import po.Organization;
import po.Useractrelation;
import service.ActivityService;
import service.OrgService;
import service.UserService;
/**
 *
 * @author HX
 */
public class CreateActivityAction extends ActionSupport implements ServletRequestAware
{
    private HttpServletRequest request;
    private ActivityService activityService;
    private OrgService orgService;
    private UserService userService;
    private Activity activity;
    private List<Integer> actorIds;
    private int orgId;
    private String timeStr;

    @Override
    public String execute()
    {
        String msg;
        if(actorIds==null)
         {
             msg="no actor ids!!!";
             request.setAttribute("msg", msg);
             return ERROR;
         }
        if (ActionContext.getContext().getSession().get("user") == null) {
            msg = "you should login first";
            request.setAttribute("msg", msg);
            return ERROR;
         }
        if(!orgService.isLeader(((User) ActionContext.getContext().getSession().get("user")).getId(), orgId)){
            msg = "you can not create an activity!";
            request.setAttribute("msg", msg);
            return ERROR;
        }
        else{
            activity.setStarttime(new Date(timeStr));
            activity.setOrganization((Organization)orgService.getOrg(orgId));
            activityService.addActivity(activity);

            Iterator it = actorIds.iterator();
            Useractrelation useractrelation;
            List<Useractrelation> useractrelations = new ArrayList<Useractrelation>();
            User u = new User();
            while(it.hasNext()){
                useractrelation = new Useractrelation();
                u = (User)userService.getUser((Integer)it.next());
                useractrelation.setUser(u);
                useractrelation.setActivity(activity);
                useractrelation.setState(Constants.UNKNOW_ABSENT);
                useractrelations.add(useractrelation);
                System.out.println(activity.getId());
                System.out.println(u.getId());
            }
            activityService.addUseractrelations(useractrelations);

            request.setAttribute("url", "getActivity.action?activityId="+ String.valueOf(activity.getId()));
            
            return SUCCESS;
        }

    }

    public void setActivityService(ActivityService activityService)
    {
        this.activityService=activityService;
    }

    public ActivityService getActivityService()
    {
        return this.activityService;
    }

    public void setOrgService(OrgService orgService)
    {
        this.orgService=orgService;
    }

    public OrgService getOrgService()
    {
        return this.orgService;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setServletRequest(HttpServletRequest request)
    {
        this.request=request;
    }

    public Activity getActivity() {
        return activity;
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }

    public void setActorIds(List<Integer> actorIds){
        this.actorIds = actorIds;
    }

    public List<Integer> getActorIds(){
        return this.actorIds;
    }

    public void setOrgId(int orgId){
        this.orgId = orgId;
    }

    public int getOrgId(){
        return this.orgId;
    }

    public String getTimeStr() {
        return timeStr;
    }

    public void setTimeStr(String timeStr) {
        this.timeStr = timeStr;
    }

}
